package xin.config.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * <pre>
 * Web MVC Config
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 15:09
 */
@Configuration
@Slf4j
public class WebAppConfigurer extends WebMvcConfigurerAdapter {

    /**
     * 配置拦截器 <br>
     * 多个拦截器组成一个拦截器链<br>
     * addPathPatterns 用于添加拦截规则<br>
     * excludePathPatterns 用户排除拦截
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ContextAdapterInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }



    /**
     * 添加自定义的静态资源映射 这里使用代码的方式自定义目录映射，并不影响Spring Boot的默认映射，可以同时使用。
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        /**
         * swagger-ui中的默认路径与spring-boot声明的默认映射不同 所以这里需要
         * 继承WebMvcConfigurerAdapter 添加自定义的静态资源映射
         */
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

//	@Bean(name = "viewResover")
//	public ViewResolver getViewResolver() {
//		lms.info("ViewResolver init");
//		lms.info(">>prefix : /WEB-INF/jsp/");
//		lms.info(">>suffix : .jsp");
//		InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
//		internalResourceViewResolver.setViewClass(JstlView.class);
//		internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
//		internalResourceViewResolver.setSuffix(".jsp");
//		return internalResourceViewResolver;
//	}
}
